package com.android.WiFiQualityMeasurement;

import java.net.InetAddress;
import android.os.AsyncTask;

public class WiFiQualityMeasurementPingAsyncTask extends AsyncTask<String, Integer, String> {

	private int mTimeout;	// Ping結果表示用
	private String mStringPingResult;

	private WiFiQualityMeasurementPingAsyncTaskPingInfo mPingInfo;

	private final String mStringSpecificAddress = "www.r-learning.co.jp";

	// コンストラクタ
	public WiFiQualityMeasurementPingAsyncTask(WiFiQualityMeasurementPingAsyncTaskPingInfo pingInfo)
	{
		mStringPingResult = "";
		mPingInfo = pingInfo;
		mTimeout = mPingInfo.getTimeout();
	}
/*
	@Override
    protected void onPreExecute() {
		mStringPingResult = "";
	}
*/
	@Override
	protected String doInBackground(String... params) {
		// TODO 自動生成されたメソッド・スタブ
		try {
	        InetAddress addr = null;
			addr = InetAddress.getByName(mStringSpecificAddress);
			long start = System.currentTimeMillis();
			if(addr != null)
			{
				boolean reachable = addr.isReachable(mTimeout);
				long end   = System.currentTimeMillis();
				if (reachable) {
					//System.out.println("到達: " + (end-start) + "ミリ秒");
					mStringPingResult += "Response: " + (end-start) + "ms";
					//mTextViewPingResult.setText("Response: " + (end-start) + "ms");
				} else {
					//System.out.println("不到達");
					mStringPingResult += "Request TimeOut";
					//mTextViewPingResult.setText("Request TimeOut");
				}
			}
		} catch (Exception e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}

		return mStringPingResult;
	}

	@Override
	protected void onPostExecute(String result) {
		mPingInfo.setText(result);
	}
}